import os
import re
from config import Config
from PIL import Image
import PIL.ImageOps


def delete_div(string_list, html):
    html = html.replace('<footer', '<div')
    html = html.replace('footer>', 'div>')
    for string in string_list:
        start = html.find(string)
        if start == -1:
            continue
        num = 1
        now = start + 1
        while num != 0:
            if html[now:now+4] == "<div":
                num += 1
            elif html[now:now+6] == "</div>":
                num -= 1
                if num == 0:
                    break
            now += 1
        html = html.replace(html[start:now+6], '')
    return html


def find_img_url(html):
    start = 0
    url_dict = dict()
    while True:
        start += 1
        start = html.find("//images.csmonitor.com/", start)
        if start == -1:
            break
        end = html.find('"', start)
        url_dict[html[start:end].split(' ')[0]] = ''
    return url_dict


def invert_png(png):
    image = Image.open(png)
    if image.mode == 'RGBA':
        r,g,b,a = image.split()
        rgb_image = Image.merge('RGB', (r,g,b))
        inverted_image = PIL.ImageOps.invert(rgb_image)
        r2,g2,b2 = inverted_image.split()
        final_transparent_image = Image.merge('RGBA', (r2,g2,b2,a))
        final_transparent_image.save(png)


script_str = '''<script>
	
	$(function()
	{
		if( $().meteredMessenger ) // not webin
		{
		    window.setTimeout(
		        function()
				{
                	$( '#sticky-messenger' ).meteredMessenger();
				}
				, 2000);
		}
	});
	
</script>'''
cookie_str = '''<div data-sticky-container class="hide">
	<div id="cookie-message"'''
page_top = '<div id="csm-page-top">'
tools_menu = '''<div class="float-right ">
					<ul class="tools-menu">'''
ezve_inbody = '<div id="ezve-inbody-newsletter-widget" class="eznid-1093460">'
footer_1 = '<div id="csm-above-footer-1">'
story_header = '<div class="story-head row">'
footer = '<div data-widget-name="footer">'

ezb_front = '<div class="ezb-frontpage_multimedia ezp-ezflow-block row">'
ezz_bottom = '<div class="ezz-bottom row small-11 medium-12"'
newsletter_block = '<div class="row small-11 medium-12"'

index_list = [ezb_front, ezz_bottom, newsletter_block]
string_list = [script_str, cookie_str, page_top, tools_menu, ezve_inbody, footer_1, story_header, footer]
replace = {
    '<div class="editor-intro small-centered eza-body small-12 medium-11">': '<div class="editor-intro small-centered eza-body small-12 medium-11" style="color: white;">',
    '<h1 id="headline" class="eza-title">': '<h1 id="headline" class="eza-title" style="color: white;">',
    '<div class="eza-caption">': '<div class="eza-caption" style="color: white;">',
    '<section class="large-10 large-offset-1 small-centered float-left paywall-position-rel columns">': '<section class="large-10 large-offset-1 small-centered float-left paywall-position-rel columns" style="color: white;">',
    '<div class="editor-container">': '<div class="editor-container" style="color: white;">',
    '<div class="columns small-12 daily logo text-center">': '<div class="columns small-12 daily logo text-center" style="background: grey;">',
    '<div class="columns small-12 daily date text-center">': '<div class="columns small-12 daily date text-center" style="background: grey;">',
    '<div class="columns small-12 medium-4 large-12 text-center daily editor ezc-staff">': '<div class="columns small-12 medium-4 large-12 text-center daily editor ezc-staff" style="background: grey;">',
    '<div class="columns small-12 medium-8 large-12 daily summary">': '<div class="columns small-12 medium-8 large-12 daily summary" style="background: grey;">',
}

os.chdir(Config.ROOT)
os.environ["http_proxy"] = "http://127.0.0.1:1080"
os.environ["https_proxy"] = "http://127.0.0.1:1080"
os.system("wget -E -c -r -p -k --no-cookies -np -l 1 https://www.csmonitor.com")
os.system("cp -rf www.csmonitor.com/* " + Config.TEMP_CSMONITOR)
os.system("rm -rf www.csmonitor.com")

with(open("templates/csmonitor/index.html")) as f:
    html = f.read()
html = delete_div(index_list, html)
with(open("templates/csmonitor/index.html", mode='w')) as f:
    f.write(html)

os.chdir(Config.TEMP_CSMONITOR)
dir_rela = dict()
dir_rela['./'] = ""
dir_list = list()
dir_list.append('./')
while len(dir_list) != 0:
    current_dir = dir_list.pop()
    for next_path in os.listdir(current_dir):
        full_path = os.path.join(current_dir, next_path)
        if os.path.isdir(full_path):
            dir_rela[full_path] = os.path.join(dir_rela[current_dir], '..')
            dir_list.append(full_path)
        elif full_path.endswith('.html'):
            with(open(full_path, mode='r')) as f:
                html = f.read()
            
            html = html.replace(script_str, '')
            html = delete_div(string_list, html)
            for key in replace.keys():
                html = html.replace(key, replace[key])

            url_dict = find_img_url(html)
            print(url_dict)
            for key in url_dict.keys():
                get_url = "https:" + key
                path = key[27:].split('/')
                file_name = path[2].replace('%', '-').replace('?', '-') + ".jpg"
                first_path = os.path.join("images", path[0])
                second_path = os.path.join(first_path, path[1])
                third_path = os.path.join(second_path, file_name)
                html_url = os.path.join(dir_rela[current_dir], second_path, file_name)

                print("{0}: {1}".format(full_path, third_path))
                html = html.replace('"{0}"'.format(key), '"{0}"'.format(html_url))
                print('replace: {0}: {1}'.format(key, html_url))

                if not os.path.exists(first_path):
                    os.mkdir(first_path)
                if not os.path.exists(second_path):
                    os.mkdir(second_path)
                if os.path.exists(third_path):
                    continue

                os.system('wget -O "{0}" "{1}"'.format(third_path, get_url))
            
            for key in url_dict.keys():
                path = key[27:].split('/')
                file_name = path[2].replace('%', '-').replace('?', '-') + ".jpg"
                first_path = os.path.join("images", path[0])
                second_path = os.path.join(first_path, path[1])
                html_url = os.path.join(dir_rela[current_dir], second_path, file_name)
                html = html.replace(key, html_url)

            with(open(full_path, mode='w')) as f:
                f.write(html)

os.chdir('/home/lrf/shark1/templates/csmonitor/var/ezflow_site/cache/public/stylesheets')
for css in os.listdir():
    if not css.endswith('.css'):
        continue

    with(open(css, mode='r')) as f:
        html = f.read()

    start = 0
    color_dict = dict()
    while True:
        start = html.find('#', start)
        if start == -1:
            break
        if re.match('^#([0-9a-fA-F]{6});$', html[start:start+8]):
            color_dict[html[start:start+7]] = 0
        elif re.match('^#([0-9a-fA-F]{3});$', html[start:start+5]):
            color_dict[html[start:start+4]] = 1
        start += 1

    for color in color_dict.keys():
        inverted = '#'
        for i in range(1, len(color)):
            if color[i] == '0':
                inverted += 'f'
            elif color[i] == '1':
                inverted += 'e'
            elif color[i] == '2':
                inverted += 'd'
            elif color[i] == '3':
                inverted += 'c'
            elif color[i] == '4':
                inverted += 'b'
            elif color[i] == '5':
                inverted += 'a'
            elif color[i] == '6':
                inverted += '9'
            elif color[i] == '7':
                inverted += '8'
            elif color[i] == '8':
                inverted += '7'
            elif color[i] == '9':
                inverted += '6'
            elif color[i] == 'a' or color[i] == 'A':
                inverted += '5'
            elif color[i] == 'b' or color[i] == 'B':
                inverted += '4'
            elif color[i] == 'c' or color[i] == 'C':
                inverted += '3'
            elif color[i] == 'd' or color[i] == 'D':
                inverted += '2'
            elif color[i] == 'e' or color[i] == 'E':
                inverted += '1'
            elif color[i] == 'f' or color[i] == 'F':
                inverted += '0'
            
        html = html.replace(color, inverted)

    html.replace('black', '#fff')
    html.replace('white', '#000')
    with(open(css, mode='w')) as f:
        f.write(html)

os.chdir(os.path.join(Config.TEMP_CSMONITOR,'extension/csm_daily/design/csm_design/images'))
for png in os.listdir():
    if png.endswith('.png'):
        invert_png(png)
os.chdir(os.path.join(Config.TEMP_CSMONITOR, 'extension/csm_base/design/csm_design/images'))
for png in os.listdir():
    if png.endswith('.png'):
        invert_png(png)
